class User < ActiveRecord::Base
  acts_as_authentic
  
  has_attached_file :firma, :styles => { :medium => "250x150>", :thumbnail => [ "80x80>", :jpg ] }
  
  has_many :note, :as => :nota_ref, :dependent => :protect
  
  validates_presence_of :fullname
  validates_attachment_size :firma, :less_than => 1.megabytes
  validates_attachment_content_type :firma, :content_type => ['image/png', 'image/jpeg']
  
  def validate
    temp_file = firma.queued_for_write[:original]
    if temp_file != nil
      dimensions = Paperclip::Geometry.from_file(temp_file)
      if !(1.5..1.8).include?(dimensions.width/dimensions.height)
        errors.add("Proporzioni non corrette, l'immagine deve essere circa 5x3cm (250x150px)")
      end
    else
      true
    end
  end
  
  # ---------------------------------------
  # The following code has been generated by role_requirement.
  # You may wish to modify it to suit your need
  has_and_belongs_to_many :roles, :join_table => 'roles_users', :uniq => true
  
  # has_role? simply needs to return true or false whether a user has a role or not.  
  # It may be a good idea to have "admin" roles return true always
  def has_role?(role_in_question)
    @_list ||= self.roles.collect(&:name)
    return true if @_list.include?("admin")
    (@_list.include?(role_in_question.to_s) )
  end
  # ---------------------------------------

  def name
    fullname
  end
end
